﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Banners;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:BannerRotator runat=server></{0}:BannerRotator>")]
    public class BannerRotator : AdRotator
    {

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsPostBack)
            {
                this.DataSource = new BannerCollection(Core.Content.Sites.SiteDefinition.GetCurrent());
                this.DataBind();
            }
        }

        private AdCreatedEventArgs _adargs;

        protected override void OnAdCreated(AdCreatedEventArgs e)
        {
            _adargs = e;
            base.OnAdCreated(e);            
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (_adargs == null)
            {
                base.Render(writer);
                return;
            }

            if (!_adargs.ImageUrl.ToLower().Contains(".swf"))
            {
                base.Render(writer);
                return;
            }

            writer.Write("<div onclick=\"window.location.href='" + _adargs.NavigateUrl + "';\">");
            writer.WriteBeginTag("object");
            writer.WriteAttribute("onclick", "window.location.href='" + _adargs.NavigateUrl + "';");
            writer.WriteAttribute("classid", "clsid:D27CDB6E-AD-11cf-96B8-444553540000");
            writer.WriteAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0");
            writer.WriteAttribute("width", _adargs.AdProperties["Width"].ToString());
            writer.WriteAttribute("height", _adargs.AdProperties["Height"].ToString());
            writer.Write(HtmlTextWriter.TagRightChar);

            writer.WriteBeginTag("param");
            writer.WriteAttribute("name", "movie");
            writer.WriteAttribute("value", _adargs.ImageUrl);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteEndTag("param");

            writer.WriteBeginTag("param");
            writer.WriteAttribute("name", "quality");
            writer.WriteAttribute("value", "high");
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteEndTag("param");

            writer.WriteBeginTag("embed");
            writer.WriteAttribute("src", _adargs.ImageUrl);
            writer.WriteAttribute("quality", "high");
            writer.WriteAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
            writer.WriteAttribute("type", "application/x-shockwave-flash");
            writer.WriteAttribute("width", _adargs.AdProperties["Width"].ToString());
            writer.WriteAttribute("height", _adargs.AdProperties["Height"].ToString());
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteEndTag("embed");


            writer.WriteEndTag("object");
            writer.Write("</div>");
            writer.Flush();
            writer.Write("</a>");
        }

    }
}
